package csci5115.autoalarm;

import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class NewDayView extends Activity {
	public static final String[] monthName = { "January", "February", "March",
			"April", "May", "June", "July", "August", "September", "October",
			"November", "December" };

	private final static long MILLISECONDS_PER_DAY = 1000L * 60 * 60 * 24;
	public static Date curDate = new Date();
	public ListView eventlistview;
	public TextView curdaytextview;
	public ArrayList<AAEvent> eventlist;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newdayview);

		// initialize views and date
		eventlistview = (ListView) findViewById(R.id.eventlist);
		if (curDate == null) {
			curDate = new Date();
		}
		RelativeLayout rl = (RelativeLayout) findViewById(R.id.top_control_bar);
		curdaytextview = (TextView) rl.findViewById(R.id.curDate);

		// Next Day Button
		Button nextDay = (Button) rl.findViewById(R.id.nextDay);
		nextDay.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				long time = curDate.getTime();
				time += MILLISECONDS_PER_DAY;
				curDate.setTime(time);

				updateLabel();
				updateList();
			}

		});

		// Previous Day Button
		Button prevDay = (Button) rl.findViewById(R.id.prevDay);
		prevDay.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				long time = curDate.getTime();
				time -= MILLISECONDS_PER_DAY;
				curDate.setTime(time);

				updateLabel();
				updateList();

			}

		});

		// add event button
		LinearLayout footer = (LinearLayout) findViewById(R.id.bottom_control_bar);
		Button addEvent = (Button) footer.findViewById(R.id.addEventButton);
		addEvent.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent myIntent = new Intent(v.getContext(), EventManager.class);
				myIntent.putExtra("date", curDate.getTime());
				startActivityForResult(myIntent, 0);

			}
		});

		// on list click
		eventlistview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				Intent myIntent = new Intent(v.getContext(), EventManager.class);
				myIntent.putExtra("date", curDate.getTime());
				myIntent.putExtra("time", eventlist.get(position).getDateLong());
				myIntent.putExtra("title", eventlist.get(position).getTitle());
				myIntent.putExtra("id", eventlist.get(position).getID());
				myIntent.putExtra("alarm", eventlist.get(position).getAlarm());
				myIntent.putExtra("location", eventlist.get(position).getLocation());
				startActivityForResult(myIntent, 0);

			}

		});
		updateLabel();
		updateList();

	}

	public void onResume() {
		super.onResume();
		updateList();
		updateLabel();
	}

	public void updateList() {
		// access event database
		EventDBAdapter db = new EventDBAdapter(this);
		db.open();
		Cursor data = db.getEventsOn(curDate);

		// Array to store events
		eventlist = new ArrayList<AAEvent>();

		if (data.getCount() > 0) {
			startManagingCursor(data);
			data.moveToFirst();
			// Add each event to event list
			while (!data.isAfterLast()) {
				AAEvent e = new AAEvent(data);
				eventlist.add(e);
				data.moveToNext();
			}
			TextView empty = (TextView) findViewById(R.id.empty);
			empty.setVisibility(TextView.INVISIBLE);
		} else {
			TextView empty = (TextView) findViewById(R.id.empty);
			empty.setVisibility(TextView.VISIBLE);
		}
		db.close();
		data.close();
		EventViewAdapter ev = new EventViewAdapter(this, eventlist);
		eventlistview.setAdapter(ev);
		eventlistview.refreshDrawableState();
	}

	public void updateLabel() {
		int day = curDate.getDate();
		int month = curDate.getMonth();
		int year = curDate.getYear() + 1900;
		curdaytextview.setText(monthName[month] + " " + Integer.toString(day)
				+ ", " + Integer.toString(year));
		curdaytextview.refreshDrawableState();
	}

}
